package com.hn658.base.message.managermq;

import javax.jms.Destination;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import com.hn658.base.message.managermq.contants.MQManagerContants;
import com.hn658.base.message.managermq.destination.IMessageDestinationBean;
import com.hn658.base.message.managermq.destination.impl.MessageDestinationQueue;
import com.hn658.base.message.managermq.destination.impl.MessageDestinationTopic;
import com.hn658.base.message.managermq.destination.impl.MqDestinationResource;
import com.hn658.framework.logging.Logger;
import com.hn658.framework.logging.LoggerFactory;
import com.hn658.framework.logging.enums.LogCategory;
@Component
public class MessageDestinationManager implements ApplicationContextAware {
	/**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(MessageListenerManager.class);
    
    private  ConfigurableApplicationContext context;
    
    private XmlBeanDefinitionReader beanDefinitionReader;
    
    public MessageDestinationManager(){

    }
    public void init(){
    	beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)  
    			context.getBeanFactory());    
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(context));   
    }
    
    public Destination get(String appId,Integer index){
    	String key = appId+"_"+index;
    	String beanName=MQManagerContants.DESTINATION+"_"+key;
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
    				"获得Spring中应用ID为：{}，MQ序号为：{}的队列", new Object[]{appId, index});
    		Object objBean = context.getBean(beanName);
    		if(objBean instanceof MessageDestinationQueue){
    			return (MessageDestinationQueue)objBean;
    		}else{
    			return (MessageDestinationTopic)objBean;
    		}
    	}else {
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中不存在应用ID为：{}，MQ队列序号为：{}的队列", new Object[]{appId, index});
    		return null;
		}
    }
    
    public void remove(String appId,Integer index){
    	String key = appId+"_"+index;
    	String beanName=MQManagerContants.DESTINATION+"_"+key;
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中删除应用ID为：{}，MQ队列序号为：{}的队列", new Object[]{appId, index});
    		Object objBean = context.getBean(beanName);
    	}else {
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中不存在应用ID为：{}，MQ队列序号为：{}的队列", new Object[]{appId, index});
		}
    }
    
    public void add(String appId,Integer index,IMessageDestinationBean messageDestinationBean){
    	String key=appId+"_"+index;
    	String beanName=MQManagerContants.DESTINATION+"_"+key;
    	messageDestinationBean.setName(beanName);
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
					"Spring中已存在应用ID为：{}，MQ队列序号为：{}的队列", new Object[]{appId, index});
			return;
    	}
    	logger.debug(LogCategory.SYSTEM,
				"Spring中增加应用ID为：{}，MQ队列序号为：{}的队列", new Object[]{appId, index});
    	beanDefinitionReader.loadBeanDefinitions(new MqDestinationResource(messageDestinationBean));
    }
    
    @Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		context = (ConfigurableApplicationContext)applicationContext;
	   	init();
	}
    
}
